import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class DDNavigatorPopPars {
  final bool canPop;
  final dynamic pars;
  DDNavigatorPopPars({this.canPop = true, this.pars});
}

Widget defaultTitle(String title) {
  return Center(
    child: Text(
      title,
      style: TextStyle(
        color: Colors.black87,
        decoration: TextDecoration.none,
        fontWeight: FontWeight.bold,
        fontSize: 16,
      ),
    ),
  );
}

class DDNavigationBar extends StatefulWidget {
  final Widget? title;
  final Color backgroud;
  final bool showBottomLine;
  final List<DDNavigationItem>? leftItems; // 额外的item
  final List<DDNavigationItem>? rightItems;
  final DDNavigatorPopPars Function()? onPop; // 点击返回的时候可以控制能否返回+返回参数
  const DDNavigationBar({
    Key? key,
    this.title,
    this.backgroud = const Color(0xffffffff),
    this.showBottomLine = true,
    this.leftItems,
    this.rightItems,
    this.onPop,
  }) : super(key: key);

  @override
  _DDNavigationBarState createState() => _DDNavigationBarState();
}

class _DDNavigationBarState extends State<DDNavigationBar> {
  bool? _showBackArrow;

  @override
  void initState() {
    super.initState();
    print("导航栏，初始化");
  }

  @override
  void didChangeDependencies() {
    if (_showBackArrow == null) {
      _showBackArrow = Navigator.of(context).canPop();
    }
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).padding.top + 44,
      width: MediaQuery.of(context).size.width,
      child: Container(
        color: widget.backgroud,
        padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
        child: Stack(
          children: items(context),
        ),
      ),
    );
  }

  List<Widget> items(BuildContext context) {
    List<Widget> list = [];
    if (_showBackArrow == true) {
      final backArrow = GestureDetector(
        onTap: _backTap,
        child: Container(
          width: 60,
          height: 44,
          child: const BackButtonIcon(),
        ),
      );
      list.add(backArrow);
    }
    if (widget.leftItems != null && widget.leftItems!.length > 0) {
      var leftMargin = 0.0;
      if (_showBackArrow == true) {
        leftMargin = 60;
      }
      Widget l = Positioned(
        left: leftMargin,
        bottom: 0,
        child: Row(
          children: widget.leftItems!,
        ),
      );
      list.add(l);
    }
    if (widget.title != null) {
      list.add(widget.title!);
    }
    if (widget.showBottomLine) {
      var divider = Positioned(
        left: 0,
        bottom: 0,
        right: 0,
        child: const Divider(height: .5, color: Colors.black26),
      );
      list.add(divider);
    }
    if (widget.rightItems != null && widget.rightItems!.length > 0) {
      Widget r = Positioned(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: widget.rightItems!,
        ),
        right: 12,
        bottom: 0,
        height: 44,
      );
      list.add(r);
    }
    return list;
  }

  // 点击返回
  void _backTap() {
    if (widget.onPop != null) {
      var can = widget.onPop!();
      if (can.canPop) {
        Navigator.of(context).pop(can.pars);
      }
      return;
    }
    Navigator.of(context).pop();
  }
}

/// 导航栏上可以添加的item
class DDNavigationItem extends StatelessWidget {
  final Widget? child;
  const DDNavigationItem({Key? key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: BoxConstraints(maxHeight: 44),
      child: child ?? Container(),
    );
  }
}
